﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class DailyWorktimeDAO : BaseDAO
    {
        public DailyWorktimeDAO()
            : base ()
        {
        }

        public BLDailyWorktimeSheet Collect(BLJobTitle jobTitleFilter, Guid agentId, string fullName, DateTime date, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLDailyWorktimeSheet result = new BLDailyWorktimeSheet(date);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_CollectDailyWorktimes", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                Guid rAgentId = (Guid)data.Rows[i]["AgentId"];
                BLDailyWorktime agentWorktime = new BLDailyWorktime(rAgentId, date);
                agentWorktime.AgentFullName = (string)data.Rows[i]["FullName"];
                agentWorktime.LeaveMorning = (int)data.Rows[i]["LeaveMorning"];
                agentWorktime.LeaveAfternoon = (int)data.Rows[i]["LeaveAfternoon"];
                agentWorktime.DataSignature = (long)data.Rows[i]["DataSignature"];
                result.AgentWorktimes.Add(agentWorktime);
            }
            return result;
        }

        public BLDailyWorktimeSheet CollectOverWorktime(BLJobTitle jobTitleFilter, Guid agentId, string fullName, DateTime date, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLDailyWorktimeSheet result = new BLDailyWorktimeSheet(date);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_CollectOverWorktimes", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                Guid rAgentId = (Guid)data.Rows[i]["AgentId"];
                BLDailyWorktime agentWorktime = new BLDailyWorktime(rAgentId, date);
                agentWorktime.AgentFullName = (string)data.Rows[i]["FullName"];
                Guid overWorktimeId = (Guid)data.Rows[i]["OverWorktimeId"];
                if (!Guid.Empty.Equals(overWorktimeId))
                {
                    BLOverWorktime overWorktime = new BLOverWorktime();
                    overWorktime.Id = overWorktimeId;
                    overWorktime.StartTime = (string)data.Rows[i]["StartTime"];
                    overWorktime.EndTime = (string)data.Rows[i]["EndTime"];
                    overWorktime.DataSignature = (long)data.Rows[i]["DataSignature"];
                    agentWorktime.OverWorktimes.Add(overWorktime);
                }
                result.AgentWorktimes.Add(agentWorktime);
            }
            return result;
        }

        public int Update(BLDailyWorktime worktime, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@agentId", worktime.AgentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", worktime.Date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@leaveAfternoon", worktime.LeaveAfternoon, ParameterDirection.Input));
            condition.Add(new BaseCondition("@leaveMorning", worktime.LeaveMorning, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", worktime.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateDailyWorktime", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int UpdateOverWorktime(BLDailyWorktime worktime, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@overWorktimeId", worktime.OverWorktimes[0].Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", worktime.AgentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", worktime.Date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startTime", worktime.OverWorktimes[0].StartTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endTime", worktime.OverWorktimes[0].EndTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", worktime.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateOverWorktime", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int AddOverWorktime(BLDailyWorktime worktime, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@overWorktimeId", worktime.OverWorktimes[0].Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", worktime.AgentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", worktime.Date, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startTime", worktime.OverWorktimes[0].StartTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endTime", worktime.OverWorktimes[0].EndTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", worktime.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_AddOverWorktime", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int DeleteOverWorktime(BLOverWorktime worktime, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@overWorktimeId", worktime.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", worktime.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteOverWorktime", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }
    }
}
